package com.fzq.orderserver.service.impl;

import com.fzq.client.ProductFeign;
import com.fzq.common.DescreaseStockInput;
import com.fzq.common.ProductInfoOutput;
import com.fzq.orderserver.dataobject.OrderDetail;
import com.fzq.orderserver.dataobject.OrderMaster;
import com.fzq.orderserver.dto.OrderDTO;
import com.fzq.orderserver.enums.OrderStatusEnum;
import com.fzq.orderserver.enums.PayStatusEnum;
import com.fzq.orderserver.repository.OrderDetailRepository;
import com.fzq.orderserver.repository.OrderMasterRepository;
import com.fzq.orderserver.service.OrderService;
import com.fzq.orderserver.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Autowired
    private OrderDetailRepository orderDetailRepository;

    @Override
    @Transactional
    public OrderDTO create(OrderDTO orderDTO) {
        String orderId = KeyUtil.genUniqueKey();
        List<String> productIdList = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());

        List<ProductInfoOutput> productInfoOutputList = productFeign.listForOrder(productIdList);
        BigDecimal orderAmout = new BigDecimal(BigInteger.ZERO);
        for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
            for (ProductInfoOutput productInfoOutput : productInfoOutputList) {
                if (productInfoOutput.getProductId().equals(orderDetail.getProductId())) {
                    orderAmout = productInfoOutput.getProductPrice().multiply(new BigDecimal(orderDetail.getProductQuantity())).add(orderAmout);
                    BeanUtils.copyProperties(productInfoOutput, orderDetail);
                    orderDetail.setOrderId(orderId);
                    orderDetail.setDetailId(KeyUtil.genUniqueKey());
                    orderDetail.setCreateTime(new Date());
                    orderDetail.setUpdateTime(new Date());
                    orderDetailRepository.save(orderDetail);
                }
            }
        }

        List<DescreaseStockInput> descreaseStockInputList = orderDTO.getOrderDetailList().stream()
                .map(e -> new DescreaseStockInput(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
        productFeign.decreaseStock(descreaseStockInputList);

        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setCreateTime(new Date());
        orderDTO.setUpdateTime(new Date());
        orderDTO.setOrderId(orderId);
        orderDTO.setOrderAmount(orderAmout);
        orderDTO.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderDTO.setPayStatus(PayStatusEnum.WAIT.getCode());
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMasterRepository.save(orderMaster);
        return orderDTO;
    }
}
